package com.bosssoft.config;



import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Aria
 */
public class NativeFeignConfig {
    @Bean
    public RequestInterceptor getRequestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
                HttpServletRequest req = servletRequestAttributes.getRequest();
                //Map<String, Collection<String>> headerMap = new HashMap();
                ////获取你需要传递的头信息
                //String userId = req.getHeader(SessionConst.KEY_USER_ID);
                //String userName = req.getHeader(SessionConst.KEY_USER_NAME);
                //String token = req.getHeader(SessionConst.KEY_TOKEN);
                //String systemRole = req.getHeader(SessionConst.KEY_USER_SYSTEMROLE_ID);
                //headerMap.put(SessionConst.KEY_USER_ID, Arrays.asList(userId));
                //headerMap.put(SessionConst.KEY_USER_NAME, Arrays.asList(userName));
                //headerMap.put(SessionConst.KEY_TOKEN, Arrays.asList(token));
                //headerMap.put(SessionConst.KEY_USER_SYSTEMROLE_ID, Arrays.asList(systemRole));
                //feign请求时，便可携带上该信息
                //requestTemplate.headers(headerMap);
            }
        };
    }
}